package com.tutorial.controller;

import com.tutorial.service.UserService;
import org.springframework.beans.factory.BeanFactoryUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.annotation.AnnotationAwareOrderComparator;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 用户信息控制层
 *
 * @ClassName UserController
 * @Description 用户信息控制层
 * @Author G
 * @Date 2025/3/4 16:08
 */
@Component
public class UserController {
    @Autowired
    private List<UserService> abc;

    public static void main(String[] args) {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        Map<String, UserService> matchingBeans = BeanFactoryUtils.beansOfTypeIncludingAncestors(context, UserService.class, true, false);
        List<UserService> list = new ArrayList<>();
        if (!matchingBeans.isEmpty()) {
            list = new ArrayList(matchingBeans.values());
            AnnotationAwareOrderComparator.sort(list);
        }
        for (UserService userService : list) {
            userService.save();
        }
        // UserService userService = context.getBean(UserService.class);
        // userService.save();
        // // userService1.save();
        // context.close();
    }

    @PostConstruct
    public void initBean() {
        System.out.println("初始化UserServiceImpl");
    }

    @PreDestroy
    public void destroyBean() {
        System.out.println("销毁UserServiceImpl");
    }
}
